package com.example.mywiseelves.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mywiseelves.R;
import com.example.mywiseelves.bean.VoiceBean;
import com.example.mywiseelves.dao.ResourcesDao;
import com.example.mywiseelves.dao.VoicesDao;
import com.example.mywiseelves.fragment.WisdomFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 进入设置智慧口令
 */
public class setWisdomActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView setWisdomBackImageView;
    EditText openBrowserEt;
    EditText openPhoneEt;
    Button setWisdomBt;


    //语音dao对象
    VoicesDao voicesDao;

    List<VoiceBean> voiceBeanList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_wisdom);
        voicesDao=new VoicesDao(this);
        findViews();
        initViews();
        updateVoice();
        setWisdomBackImageView.setOnClickListener(this);
        setWisdomBt.setOnClickListener(this);
    }

    /**
     * 实例化控件
     */
    private void findViews() {
        setWisdomBackImageView = findViewById(R.id.setWisdom_back_iv);
        openBrowserEt= findViewById(R.id.openBrowser_et);
        openPhoneEt= findViewById(R.id.openPhone_et);
        setWisdomBt= findViewById(R.id.setWisdom_bt);
    }
    /**
     * 初始化控件
     */
    private void initViews() {
        voiceBeanList=voicesDao.allVoiceQuery();
        openBrowserEt.setText(voiceBeanList.get(0).getVoice());
        openPhoneEt.setText(voiceBeanList.get(1).getVoice());
        myFilter(openBrowserEt);
        myFilter(openPhoneEt);
    }

    /**更改口令*/
    public void updateVoice(){
        String openBrowser=openBrowserEt.getText().toString();
        String openPhone=openPhoneEt.getText().toString();
        voicesDao.voicesUpdate(0,openBrowser);
        voicesDao.voicesUpdate(1,openPhone);
    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.setWisdom_back_iv) {
            finish();
        }else if (v.getId() == R.id.setWisdom_bt){
            if (openBrowserEt.getText().length()>5||openPhoneEt.getText().length()>5){
                Toast.makeText(this,"启动口令不要大于5位",Toast.LENGTH_LONG).show();
            }else if (openBrowserEt.getText().length()==0||openPhoneEt.getText().length()==0){
                Toast.makeText(this,"启动口令不要设置为空",Toast.LENGTH_LONG).show();
            } else {
                updateVoice();
                Toast.makeText(this,"修改口令成功，请重启精灵",Toast.LENGTH_LONG).show();
            }
        }
    }
    /**
     * 指定输入
     */
    public void myFilter(EditText et) {
        InputFilter filter = (source, start, end, dest, dstart, dend) -> {
            for (int i = start; i < end; i++) {
                //判断字母还是数字
                if (!Character.isLetterOrDigit(source.charAt(i))) {
                    return "";
                }
                String speChat = "[\\u4e00-\\u9fa5]";
                //Pattern类的compile方法用于编译一个正则表达式并返回一个编译好的pattern对象
                Pattern pattern = Pattern.compile(speChat);
                //Pattern类的matcher方法用于封装一个要操作的字符串并返回Matcher对象，用于操作字符串
                Matcher matcher = pattern.matcher(source.toString());
                //Matcher类的matches方法用判断字符串和正则表达式是否匹配
                if (!matcher.matches()) {
                    return "";
                } else {
                    return null;
                }
            }
            return null;
        };
        et.setFilters(new InputFilter[]{filter});
    }
}